﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;


namespace UI.Admin.Model
{
    /// <summary>
    /// AjaxAction 的摘要说明
    /// </summary>
    public class AjaxAction : IHttpHandler,IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //验证操作权限
           // string auth = Utility.Authorization.Check(HttpContext.Current.Session["role"], "ModelAdmin", "");
            //if (auth != "ACCEPT") { return; }
            context.Response.ContentType = "text/plain";
            string dowhat = context.Request["dowhat"];
            if (dowhat == null) dowhat = "";

            //检查操作用户和IP
            string UserName="test";//((Models.User)(HttpContext.Current.Session["user"])).Name;
            string UserIP = "1314";//context.Request.ServerVariables["LOCAL_ADDR"];

            #region 添加模型
            if (dowhat == "add_model")
            {
                string Name = context.Request["Name"];
                string AdminDir = context.Request["AdminDir"];
                string WebDir = context.Request["WebDir"];
                string TableName = context.Request["TableName"];
                string Remark = context.Request["Remark"];

                if (BLL.Create.CreateModel(Name,AdminDir, WebDir, TableName, Remark)>0)
                {
                    //记录成功操作到日志
                    BLL.Create.CreateLogRecord(UserName, UserIP, "添加了一个" + Name);
                    context.Response.Write("success");
                }
                else
                {
                    context.Response.Write("failed");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 修改模型
            if (dowhat == "update_model")
            {
                string ID = context.Request["ID"];
                string Name = context.Request["Name"];
                string AdminDir = context.Request["AdminDir"];
                string WebDir = context.Request["WebDir"];
                string TableName = context.Request["TableName"];
                string Remark = context.Request["Remark"];

                if (BLL.Update.UpdateModel(ID, Name, AdminDir, WebDir, TableName, Remark) > 0)
                {
                    //记录操作到日志
                    BLL.Create.CreateLogRecord(UserName, UserIP, "修改了" + Name);
                    context.Response.Write("success");
                }
                else { context.Response.Write("failed"); }
                context.Response.End();
                return;
            }
            #endregion

            #region 删除模型
            if (dowhat == "delete_model")
            {
                string ID = context.Request["ID"];

                string Name = BLL.Find.FindModel(ID).Name;

                if (BLL.Delete.DeleteModel(ID) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "删除了" + Name );
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("failed");
                }
                context.Response.End();
                return;
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}